"""
    客户端试图界面模块
"""
from socket import *
ADDR = ('127.0.0.1',7977)
class View:
    def __init__(self):
        self.s = socket()
        self.s.connect(ADDR)
    def handle(self,tag,username,password='#',comment='#'):
        str = ' '.join([tag,username,password,comment])
        self.s.send(str.encode())
        data = self.s.recv(1024)
        if not data:
            return 'server close...'
        return data.decode()
    def test(self):
        while True:
            print("""
        **英英单词字典**
            1.登陆
            2.注册
            3.退出""")
            msg = input('清输入选择>>')
            if not msg or msg == '3':
                print('客户端已退出！')
                break
            if msg == '2':
                user = input('清输入用户名：')
                password = input('清输入密码：')
                comment = input('清输入备注：')
                data = self.handle("R",user,password,comment)
                if data == 'ok':
                    print('注册成功!')
                else:
                    print('注册失败：',data)
            if msg == '1':
                user = input('清输入用户名：')
                password = input('清输入密码：')
                data = self.handle("L",user,password)
                if data == 'ok':
                    print('登陆成功!')
                    while True:
                        print("""
                                **英英单词字典**
                                    1.查询单词
                                    2.历史记录
                                    3.注销""")
                        msg = input('清输入选择>>')
                        if not msg or msg == '3':
                            print('已返回主菜单！')
                            break
                        elif msg == '1':
                            while True:
                                word = input('请输入要查询的单词或按Q退出：')
                                if word == 'q' or not word:
                                    break
                                data = self.handle('W',user,word)
                                print(data)
                        elif msg == '2':
                            data = self.handle('J',user)
                            print(data)
                else:
                    print('登陆失败：',data)
        self.s.close()

if __name__ == '__main__':
    c = View()
    c.test()
